CC = gcc
CFLAGS = -I.  -I../Core/Inc -Wall -Wextra -g -fprofile-arcs -ftest-coverage -DUNIT_TEST 
LDFLAGS = -lgcov

UNITY_SRC = unity/unity.c
TEST_SRC = modbus_tcp/mock_fuctions.c modbus_tcp/test_modubus_tcp.c
SRC = ../Core/Src/modbus_tcp.c ../Core/Src/utils.c

UNITY_OBJ = $(UNITY_SRC:.c=.o)
TEST_OBJ = $(TEST_SRC:.c=.o)
OBJ = $(SRC:.c=.o)

TARGET = run_tests

.PHONY: all clean test coverage

all: $(TARGET)








$(TARGET): $(UNITY_OBJ) $(TEST_OBJ) $(OBJ)
	$(CC) -o $@ $^ $(LDFLAGS)

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

test: $(TARGET)
	./$(TARGET)

coverage: test
	gcov $(SRC)
	@mkdir -p coverage_report
	lcov --capture --directory . --directory ../Core/Src --output-file coverage.info
	lcov --remove coverage.info '/usr/*' --output-file coverage.info
	genhtml coverage.info --output-directory coverage_report
	@echo "覆盖率报告已生成在 coverage_report 目录"
	@echo "请打开 coverage_report/index.html 查看详细报告"
clean:
	rm -f $(TARGET) $(UNITY_OBJ) $(TEST_OBJ) $(OBJ)
	rm -f *.gcno *.gcda *.gcov
	rm -f ../modbus_tcp/*.gcno ../modbus_tcp/*.gcda ../modbus_tcp/*.gcov
	rm -rf coverage_report/*
